﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using System.IO;
using CNVP.Config;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;
using System.Text.RegularExpressions;

namespace CNVP.JShop.Web.Member
{
    public partial class AdEdit : UserPage
    {
        BLL.CNVP_Ads bll = new BLL.CNVP_Ads();
        Model.CNVP_Ads model = new Model.CNVP_Ads();
        public int id = 0;
        public string picOriginal = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }

            if (!IsPostBack)
            {
                txtPostTime.Attributes.Add("ReadOnly", "true");
                txtOpenTime.Attributes.Add("ReadOnly", "true");
                if (id > 0)
                {
                    lbTitle.Text = " > 修改套黄广告";
                    DataInfo(id);
                    optype.Value = "1";
                    aid.Value = id.ToString();

                    txtLServerName.Attributes.Add("ReadOnly", "true");
                    txtDownLoadUrl.Attributes.Add("ReadOnly", "true");
                    txtHomeLink.Attributes.Add("ReadOnly", "true");
                    txtHomeLinkUrl.Attributes.Add("ReadOnly", "true");

                }
                else
                {
                    lbTitle.Text = " > 添加套黄广告";
                    optype.Value = "0";
                    aid.Value = "0";
                    
                }
                
            }

        }

        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_AdsById(id);
            if (model != null)
            {
                txtServerName.Text = model.ServerName;
                txtLServerName.Text = model.DownLoadLink;
                txtDownLoadUrl.Text = model.DownLoadUrl;
                txtOpenTime.Text = model.OpenTime;
                txtEditionInfor.Text = model.EditionInfor;
                txtHomeLink.Text = model.HomeLink;
                txtHomeLinkUrl.Text = model.HomeLinkUrl;
                txtPostTime.Text = Public.StampToDate(long.Parse(model.AdvanceTime)).ToString();

                rblIsShow.SelectedValue = model.IsShow.ToString();
               // txtOrderID.Text = model.OrderID.ToString();
                ddlDisplayID.SelectedValue = model.DisplayType.ToString();
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            txtPostTime.Attributes.Add("ReadOnly", "false");
            txtOpenTime.Attributes.Add("ReadOnly", "false");
            DateTime OverdueTime = Convert.ToDateTime(txtPostTime.Text.Trim());
            DateTime nowTime = DateTime.Now;
            TimeSpan npSpan = nowTime - OverdueTime;
            if (npSpan.Seconds >= 1)
            {
                MessageUtils.ShowPre("您选择的时间已过期！");
            }

            if (id > 0)
            {
                txtServerName.Attributes.Add("ReadOnly", "false");
                txtLServerName.Attributes.Add("ReadOnly", "false");
                txtDownLoadUrl.Attributes.Add("ReadOnly", "false");
                txtHomeLink.Attributes.Add("ReadOnly", "false");
                txtHomeLinkUrl.Attributes.Add("ReadOnly", "false");
                string oldtime = new BLL.CNVP_Ads().GetSingle_CNVP_AdsById(id).AdvanceTime;

                string editrsult = Credit_forEdit(id);

                //修改时 editrsult为2 为置顶或未修改 置顶时间   ，设置置顶时间为原来的时间
                if (editrsult=="2")
                {
                     #region 修改
                     int rows = new BLL.CNVP_Ads().Update_CNVP_Ads(
                       new Model.CNVP_Ads()
                       {
                           ID = id,
                           IsType = 0,
                           ServerName = txtServerName.Text.Trim(),
                           DownLoadLink = txtLServerName.Text.Trim(),
                           DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                           OpenTime = txtOpenTime.Text.Trim(),
                           EditionInfor = txtEditionInfor.Text.Trim(),
                           HomeLink = txtHomeLink.Text.Trim(),
                           HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                           AdvanceTime = oldtime,

                           IsShow = byte.Parse(rblIsShow.SelectedValue),
                          // OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                           AdClassID = 105,
                           DisplayType = ddlDisplayID.SelectedIndex,   //0为：普通套黄；1为：通宵固顶；2为：全天固定；3通宵广告
                           AdminID = CookieUserID

                       }

               );
                if (rows == 1)
                {
                    BasePage.MessageBox("修改成功！", "AdvanceAds.aspx?Page=1");
                }
                else
                { BasePage.MessageBoxGoHistory("修改失败！"); }
                #endregion
                }
                else if (editrsult == "1")
                {
                    #region 修改
                    int rows = new BLL.CNVP_Ads().Update_CNVP_Ads(
                      new Model.CNVP_Ads()
                      {
                          ID = id,
                          IsType = 0,
                          ServerName = txtServerName.Text.Trim(),
                          DownLoadLink = txtLServerName.Text.Trim(),
                          DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                          OpenTime = txtOpenTime.Text.Trim(),
                          EditionInfor = txtEditionInfor.Text.Trim(),
                          HomeLink = txtHomeLink.Text.Trim(),
                          HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                          AdvanceTime = Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim())).ToString(),

                          IsShow = byte.Parse(rblIsShow.SelectedValue),
                         // OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                          AdClassID = 105,
                          DisplayType = ddlDisplayID.SelectedIndex,   //0为：普通套黄；1为：通宵固顶；2为：全天固定；3通宵广告
                          AdminID = CookieUserID

                      }

              );
                    if (rows == 1)
                    {
                        BasePage.MessageBox("修改成功！", "AdvanceAds.aspx?Page=1");
                    }
                    else
                    { BasePage.MessageBoxGoHistory("修改失败！"); }
                    #endregion
                }
                else
                {
                    MessageUtils.Show("您的余额不足，请及时充值！");
                }
            }
            else
            {
                string addresult =  Credit_forAdd();
                //addresult 为2 表示添加普通套黄或通宵广告  4表示添加了 [全天置顶] 或 [通宵置顶]广告
                //addresult 为1 表示添加全天或通宵时出现积分不足时  系统默认他的广告类别为[普通套黄]
                if (addresult == "2" || addresult == "4")
                {
                    #region 添加

                    int rows = new BLL.CNVP_Ads().Add_CNVP_Ads(new Model.CNVP_Ads()
                    {
                        ID = id,
                        IsType = 0,
                        ServerName = txtServerName.Text.Trim(),
                        DownLoadLink = txtLServerName.Text.Trim(),
                        DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                        OpenTime = txtOpenTime.Text.Trim(),
                        EditionInfor = txtEditionInfor.Text.Trim(),
                        HomeLink = txtHomeLink.Text.Trim(),
                        HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                        AdvanceTime = Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim())).ToString(),


                        IsShow = byte.Parse(rblIsShow.SelectedValue),
                       // OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                        AddTime = DateTime.Now,
                        AdClassID = 105,
                        DisplayType = ddlDisplayID.SelectedIndex,
                        AdminID = CookieUserID
                    }, 1);

                    if (rows == 1)
                    {
                        BasePage.MessageBox("添加成功！", "AdvanceAds.aspx");
                    }
                    else
                    { BasePage.MessageBoxGoHistory("添加失败！"); }
                    #endregion
                }
                else if (addresult=="1")
                {
                    #region 添加

                    int rows = new BLL.CNVP_Ads().Add_CNVP_Ads(new Model.CNVP_Ads()
                    {
                        ID = id,
                        IsType = 0,
                        ServerName = txtServerName.Text.Trim(),
                        DownLoadLink = txtLServerName.Text.Trim(),
                        DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                        OpenTime = txtOpenTime.Text.Trim(),
                        EditionInfor = txtEditionInfor.Text.Trim(),
                        HomeLink = txtHomeLink.Text.Trim(),
                        HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                        AdvanceTime = Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim())).ToString(),


                        IsShow = byte.Parse(rblIsShow.SelectedValue),
                       // OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                        AddTime = DateTime.Now,
                        AdClassID = 105,
                        DisplayType = 0,
                        AdminID = CookieUserID
                    }, 1);

                    if (rows == 1)
                    {
                        BasePage.MessageBox("由于您的积分不足系统将您添加的广告设置为普通套黄广告！", "AdvanceAds.aspx");
                    }
                    else
                    { BasePage.MessageBoxGoHistory("添加失败！"); }
                    #endregion
                }
                else if (addresult == "3")
                {
                    MessageUtils.Show("添加失败！");
                }
                else
                {
                    MessageUtils.Show("您的余额不足，请及时充值！");
                }
            }
            
                
           
        }

        #region 广告添加-[积分日志]
        public string Credit_forAdd()
        {
            Model.CNVP_Users md = new BLL.CNVP_Users().GetSingle_CNVP_UsersById(CookieUserID);
            if (md.PointsNum > 0)
            {
                string adtype = ddlDisplayID.SelectedIndex.ToString();
                string adAddRate = SchemeRules.GetConfigValue("Add");
                string alldate = SchemeRules.GetConfigValue("Day");
                string allnoght = SchemeRules.GetConfigValue("Night");

                if (md.PointsNum - int.Parse(adAddRate) >= 0)
                {
                    int oprow = new BLL.CNVP_CreditInflux().Add_CNVP_CreditInflux(new Model.CNVP_CreditInflux
                    {
                        OperatorID = CookieUserID,
                        OperatTime = DateTime.Now.ToString(),
                        OperaType = 0,
                        OperaTargID = "0",
                        QriginalCredit = md.PointsNum.ToString(),
                        PresentCredit = (md.PointsNum - int.Parse(adAddRate)).ToString(),
                        Remark = ""
                    });
                    
                    if (oprow == 1)
                    {
                        new BLL.CNVP_Users().UpdatePointsNumType(CookieUserID, int.Parse(adAddRate),0);

                        if (adtype == "1")
                        {
                            if (md.PointsNum - int.Parse(alldate) >= 0)
                            {
                                int oprow2 = new BLL.CNVP_CreditInflux().Add_CNVP_CreditInflux(new Model.CNVP_CreditInflux
                                {
                                    OperatorID = CookieUserID,
                                    OperatTime = DateTime.Now.ToString(),
                                    OperaType = 0,
                                    OperaTargID = "0",
                                    QriginalCredit = md.PointsNum.ToString(),
                                    PresentCredit = (md.PointsNum - int.Parse(alldate)).ToString(),
                                    Remark = ""
                                });
                                if (oprow2 == 1)
                                {
                                    new BLL.CNVP_Users().UpdatePointsNumType(CookieUserID, int.Parse(alldate), 0);
                                    return "4";
                                }
                                else
                                {
                                    return "3";
                                }
                            }
                            else
                            {
                                return "1";
                            }
                        }
                        else if (adtype == "2")
                        {
                            if (md.PointsNum - int.Parse(allnoght) >= 0)
                            {
                                int oprow3 = new BLL.CNVP_CreditInflux().Add_CNVP_CreditInflux(new Model.CNVP_CreditInflux
                                {
                                    OperatorID = CookieUserID,
                                    OperatTime = DateTime.Now.ToString(),
                                    OperaType = 0,
                                    OperaTargID = "0",
                                    QriginalCredit = md.PointsNum.ToString(),
                                    PresentCredit = (md.PointsNum - int.Parse(allnoght)).ToString(),
                                    Remark = ""
                                });
                                if (oprow3 == 1)
                                {
                                    new BLL.CNVP_Users().UpdatePointsNumType(CookieUserID, int.Parse(allnoght), 0);
                                    return "4";
                                }
                                else
                                {
                                    return "3";
                                }
                            }
                            else
                            {
                                return "1";
                            }
                        }
                        else
                        {
                            return "2";
                        }
                    }
                    else
                    {
                        return "3";
                    }
                }
                else
                {
                    return "0";
                }
            }
            else
            { 
                return "0";
            }
        }
        #endregion

        #region 广告修改-[积分日志]
        public string Credit_forEdit(int id)
        {
            string adEditRate = SchemeRules.GetConfigValue("Edit");
            Model.CNVP_Users md = new BLL.CNVP_Users().GetSingle_CNVP_UsersById(CookieUserID);
            if (md.PointsNum > 0)
            {
                Model.CNVP_Ads umd = new BLL.CNVP_Ads().GetSingle_CNVP_AdsById(id);
                string toptime = umd.AdvanceTime;
                if (Public.GetTimeStamp() - long.Parse(toptime) > 0)
                {
                    /*未置顶*/
                    return "2";
                }
                else
                { 
                    /*置顶过后*/
                    int ps2 = 0;
                    long enterytime =  Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim()));
                    if (enterytime == long.Parse(toptime))
                    {
                        return "2";
                    }
                    else
                    {
                        if (txtServerName.Text.Trim() == umd.ServerName)
                        {
                            ps2 = int.Parse(adEditRate) / 2;
                            return "2";
                        }
                        else
                        {
                            ps2 = int.Parse(adEditRate);
                        }
                        new BLL.CNVP_Users().UpdatePointsNumType(CookieUserID, ps2, 0);

                        int oprow = new BLL.CNVP_CreditInflux().Add_CNVP_CreditInflux(new Model.CNVP_CreditInflux
                        {
                            OperatorID = CookieUserID,
                            OperatTime = DateTime.Now.ToString(),
                            OperaType = 1,      //0添加  ，1修改
                            OperaTargID = id.ToString(),
                            QriginalCredit = md.PointsNum.ToString(),
                            PresentCredit = (md.PointsNum - ps2).ToString(),
                            Remark = ""
                        });
                        return "1";
                    }
                }
            }
            else 
            {
                return "0";
            }
        }
        #endregion
    }
}